package org.tinygroup.tinypc.test.plus;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import org.tinygroup.logger.LogLevel;
import org.tinygroup.logger.Logger;
import org.tinygroup.logger.LoggerFactory;
import org.tinygroup.tinypc.Warehouse;
import org.tinygroup.tinypc.Work;
import org.tinygroup.tinypc.WorkSplitter;
import org.tinygroup.tinypc.Worker;
import org.tinygroup.tinypc.impl.WarehouseDefault;

public class PlusWorkSplitter implements WorkSplitter {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7586550309287462029L;
	private static final Logger logger = LoggerFactory
			.getLogger(PlusWorkSplitter.class);

	public List<Warehouse> split(Work work, List<Worker> workers)
			throws RemoteException {
		int[] params = work.getInputWarehouse().get(PlusWork.PARAM);
		int workerSize = workers.size();
		return deal(params, workerSize);
	}

	private List<Warehouse> deal(int[] params, int workerSize) {
		List<Warehouse> list = new ArrayList<Warehouse>();

		int length = params.length;
		int eachLength = length / workerSize;
		int moreLength = length % workerSize;
		int begin = 0;
		for (int i = 0; i < workerSize; i++) {
			Warehouse warehouse = new WarehouseDefault();
			int thisLength = eachLength;
			if (i < moreLength) {
				thisLength++;
			}
			logger.logMessage(LogLevel.INFO, "position{},{}begin,length{}", i,
					begin, thisLength);
			warehouse.put(PlusWork.PARAM,
					getIntArray2(params, begin, thisLength));
			begin += thisLength;
			logger.logMessage(LogLevel.INFO, "position{},warehouse:{}", i,
					warehouse);
			logger.logMessage(LogLevel.INFO, "position{},warehouse param:{}",
					i, getString((int[]) warehouse.get(PlusWork.PARAM)));
			list.add(warehouse);
		}
		return list;
	}

	private int[] getIntArray(int[] params, int begin, int end) {
		if (end <= begin) {
			throw new IndexOutOfBoundsException("end必须大于begin");
		}
		int[] newArray = new int[end - begin];
		for (int i = 0; i < end - begin; i++) {
			newArray[i] = params[begin + i];
		}
		return newArray;
	}

	private int[] getIntArray2(int[] params, int begin, int length) {
		if (length <= 0) {
			throw new IndexOutOfBoundsException("end必须大于begin");
		}
		int[] newArray = new int[length];
		for (int i = 0; i < length; i++) {
			newArray[i] = params[begin + i];
		}
		return newArray;
	}

	private String getString(int[] array) {
		String s = "";
		for (int i : array) {
			s = s + i + ",";
		}
		return s;
	}

}
